package com.kll.tv.views.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.kll.tv.R;
import com.kll.tv.databinding.AlertDatePickerDialogBinding;
import com.kll.tv.utils.CalendarUtil;

import java.util.Calendar;
import java.util.Date;

/**
 * 日期控制器，可以选择年月日以及清空按钮的显示与隐藏，可以限制时间的选择范围。
 * Created by lwt on 2017/11/23.
 */

public class DateChooseDialog implements View.OnClickListener {

    private AlertDatePickerDialogBinding binding;
    private AlertDialog alert;
    private Context context;

//    private DatePicker mDatePicker;
    private MyOnDateSetListener onDateSetListener;
//    private Button cancleButton, clearButton, okButton;
//    private LinearLayout clearLl;

    // 控制 日期
    private int measureWidth;

    // 是否 显示 日选择器   true 显示 ，false 隐藏
    private boolean isDayVisible = true;
    // 是否 显示 月选择器   true 显示 ，false 隐藏
    private boolean isMonthVisible = true;
    // 是否 显示 年选择器   true 显示 ，false 隐藏
    private boolean isYearVisible = true;
    // 是否 显示 清空按钮   true 显示 ，false 隐藏
    private boolean isClearVisible = true;

//    protected DateChooseDialog(Context context) {
//        super(context);
//    }

//    protected DateChooseDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
//        super(context, cancelable, cancelListener);
//    }

//    protected DateChooseDialog(Context context, @StyleRes int themeResId) {
//        super(context, themeResId);
//    }

    /**
     * @param context        上下文对象
     * @param callBack       选择 监听器//        var alert = AlertDialog.createCustom(this, R.layout.alert_gy_annealing_add)
     * //
     * //        var timeTv = alert.findViewById<TextView>(R.id.tv_time)
     * //        var widthEdt = alert.findViewById<EditText>(R.id.edt_width)
     * //        var speedEdt = alert.findViewById<EditText>(R.id.edt_speed)
     * //        var boardSurfaceEdt = alert.findViewById<EditText>(R.id.edt_board_surface)
     *
     *         val format = DateFormat.getDateTimeInstance()
     *         val calendar = Calendar.getInstance(Locale.CHINA)
     *
     *         TimePickerDialog(this, 2, object: TimePickerDialog.OnTimeSetListener {
     *             override fun onTimeSet(p0: TimePicker?, p1: Int, p2: Int) {
     *
     *             }
     *
     *         }// 设置初始时间
     *         , calendar.get(Calendar.HOUR_OF_DAY)
     *         , calendar.get(Calendar.MINUTE)
     *         // true表示采用24小时制
     *         ,true).show()
     * @param isYearVisible  年 是否可见
     * @param isMonthVisible 月 是否可见
     * @param isDayVisible   日 是否可见
     */
    public DateChooseDialog(Context context, MyOnDateSetListener callBack, boolean isYearVisible, boolean isMonthVisible, boolean isDayVisible, boolean isClearVisible ) {
//        super(context);
        this.context = context;
        this.isDayVisible = isDayVisible;
        this.isMonthVisible = isMonthVisible;
        this.isYearVisible = isYearVisible;
        this.isClearVisible = isClearVisible;
        this.onDateSetListener = callBack;
        init(false);
    }

    public DateChooseDialog(Context context, MyOnDateSetListener callBack, boolean isAfter ) {
//        super(context);
        this.context = context;
        this.isClearVisible = false;
        this.onDateSetListener = callBack;
        init(true);
    }

    private void init(boolean isAfter) {

        binding = AlertDatePickerDialogBinding.inflate(LayoutInflater.from(context));

        alert = AlertDialog.createCustom(context, binding.getRoot());

        Calendar mCalendar = Calendar.getInstance();
        mCalendar.setTime(new Date());

        binding.cancelButton.setOnClickListener(this);
        binding.clearButton.setOnClickListener(this);
        binding.okButton.setOnClickListener(this);

        if(isAfter) { // 可以选之后
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.YEAR, -1);
            binding.datePicker.setMinDate(calendar.getTime().getTime());//最早一年前
            calendar.add(Calendar.YEAR, 2);
            binding.datePicker.setMaxDate(calendar.getTime().getTime());//最晚一年后
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.YEAR, -3);
            binding.datePicker.setMinDate(calendar.getTime().getTime());//最早3年前
            binding.datePicker.setMaxDate(new Date().getTime());//最晚现在
        }

        // 是否 显示 年
        if (!this.isYearVisible) {
            ((ViewGroup) ((ViewGroup) binding.datePicker.getChildAt(0)).getChildAt(0)).getChildAt(0).setVisibility(View.GONE);
        }

        // 是否 显示 月
        if (!this.isMonthVisible) {
            ((ViewGroup) ((ViewGroup) binding.datePicker.getChildAt(0)).getChildAt(0)).getChildAt(1).setVisibility(View.GONE);
        }

        // 是否 显示 日
        if (!this.isDayVisible) {
            ((ViewGroup) ((ViewGroup) binding.datePicker.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
        }

        // 是否 显示 清空按钮
        if (!this.isClearVisible) {
            binding.clearLl.setVisibility(View.GONE);
        }


        // 设置 显示 宽高
        int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

        binding.buttonGroup.measure(width, height);
        binding.datePicker.measure(width, height);
        if (binding.buttonGroup.getMeasuredWidth() > binding.datePicker.getMeasuredWidth()) {
            this.measureWidth = binding.buttonGroup.getMeasuredWidth();
        } else {
            this.measureWidth = binding.datePicker.getMeasuredWidth();
        }
//        Log.i("testss", this.measureWidth + "measuredWidth");
    }

    public static String formatDate(String date){

        return date != null && date.length() >= 10 ? date.substring(0, 10) : date;
    }

    /***
     * 设置日期
     * @param date
     */
    public DateChooseDialog setDate(String date){
        Date time = null;
        if(date.isEmpty()){
            time = new Date(System.currentTimeMillis());
        } else {
            time = CalendarUtil.toDate(date);
        }

        binding.datePicker.init(CalendarUtil.getYear(time), CalendarUtil.getMonth(time), CalendarUtil.getDay(time), null);
        return this;
    }

    public void hideOrShow() {
        if (this == null) {
            return;
        }
        if (!alert.isShowing()) {
            alert.show();
            //设置 显示 的 宽高
            WindowManager.LayoutParams attributes = alert.getWindow().getAttributes();
            attributes.width = this.measureWidth + 100;
            alert.getWindow().setAttributes(attributes);
        } else {
            alert.dismiss();
        }
    }

    public void show(){
        alert.show();
    }

    @Override
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.cancelButton) {
            alert.dismiss();

        } else if (i == R.id.clearButton) {
            onDateSetListener.onDateSet(null);
            alert.dismiss();

        } else if (i == R.id.okButton) {
            onOkButtonClick();
            alert.dismiss();

        }
    }

    /**
     * 确认 按钮 点击 事件
     */
    private void onOkButtonClick() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, binding.datePicker.getYear());
        calendar.set(Calendar.MONTH, binding.datePicker.getMonth());
        calendar.set(Calendar.DAY_OF_MONTH, binding.datePicker.getDayOfMonth());
        calendar.getTime();
        if (onDateSetListener != null) {
            onDateSetListener.onDateSet(calendar.getTime());
        }
        }

    /**
     * 时间  改变 监听
     */
    public interface MyOnDateSetListener {
        void onDateSet(Date date);
    }
}
